The SaxDigesterHandler is the SAX xml handler that allows registration of ElementDigester, which operates on one particular xml element. For example, a typical xml parsing using the digester has the following structure
//setup the digester SaxDigesterHandler handler = new SaxDigesterHandler(); handler.addDigester(new MyRootElementDigester()); handler.addDigester("path/to/element", new MyElementDigester()); handler.addDigester(...); handler.addDigester(...); handler.addDigester(...); // Parse the input SAXParser saxParser = factory.newSAXParser(); saxParser.parse(in, handler);
public interface ElementDigester { //the element name observed by this digester public String getElementName(); //when the element is encountered public void startDigest(SaxDigesterHandler handler, Attributes attr) throws SAXException; //when the element ends public void endDigest(SaxDigesterHandler handler) throws SAXException; //when theres CDATA in the element public void characters(SaxDigesterHandler handler, char[] chars, int offset, int length) throws SAXException; }
<xml> <myroot> <customizable class="userclass"> .... userclass specific xml segment here... </customizable> </myroot> </xml>
public class CustomizableDigester { //... other code section here ... //when the element is encountered public void startDigest(SaxDigesterHandler handler, Attributes attr) { String class = attr.getValue("class"); Object userObj = ... //user object instantiate the object from class attribute handler.addSubDigester((ElementDigester)userObj); }; }